package com.htx.zqs.blesmartmask.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.bean.EventBean;
import com.htx.zqs.blesmartmask.eventbus.MessageEvent;
import com.htx.zqs.blesmartmask.utils.Constants;
import com.htx.zqs.blesmartmask.utils.EventBusUtil;
import com.htx.zqs.blesmartmask.utils.MySpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

/**
 * Created by KomoriWu
 * 上传设备使用时间、次数
 * on 2019/3/9.
 */


public class UpTimesService extends Service {
    public static final String NAME = "com.htx.zqs.blesmartmask.service.UpTimesService";
    public static final int UPDATE_DELAY = 0;
    public static final int UPDATE_PERIOD = 1;
    public static final int UPDATE_INTERVAL = 9;
    private Disposable mDisposable;
    private int index;
    private int mDelay;

    @Override
    public void onCreate() {
        super.onCreate();
        Timber.e("UpTimesService onCreate");
        EventBusUtil.register(this);
        mDelay = UPDATE_DELAY;
        start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void start() {
        Observable.interval(mDelay, UPDATE_PERIOD, TimeUnit.MINUTES)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {
                        if (aLong == 0) return;
                        mDelay = UPDATE_INTERVAL - (UPDATE_INTERVAL / aLong.intValue());
                        if (aLong % UPDATE_INTERVAL == 0) {
                            Timber.e("上传设备使用时间、次数aLong：" + aLong);
                            //9分钟上传一次
                            if (MySpUtils.getBoolean(MySpUtils.KEY_STAR_AUTO_INCREMENT, false)) {
                                int num = MySpUtils.getInt(MySpUtils.KEY_DEVICE_USE_NUM, -1);
                                int time = MySpUtils.getInt(MySpUtils.KEY_DEVICE_USE_TIME, -1);
                                time += 10;
                                index += 1;
                                if (index % 2 == 0) {
                                    num += 1;
                                }
                                MySpUtils.putInt(MySpUtils.KEY_DEVICE_USE_TIME, time);
                                MySpUtils.putInt(MySpUtils.KEY_DEVICE_USE_NUM, num);
                                EventBus.getDefault().post(new MessageEvent("",
                                        Constants.EVENT_UP_DEVICE_INFO));
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void dispose() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable = null;
            Timber.i("dispose");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Timber.e("UpTimesService onDestroy");
        EventBusUtil.unregister(this);
    }

    @Subscribe
    public void onEventMainThread(EventBean eventBean) {
        if (eventBean != null) {
            switch (eventBean.getCode()) {
                case Constants.EVENT_START_QUERY_SERVICE:
                    if (mDisposable == null) {
                        Timber.i("启动UpTimesService的定时器");
                        start();
                    }
                    break;
                case Constants.EVENT_PAUSE_QUERY_SERVICE:
                    Timber.i("暂停UpTimesService的定时器");
                    dispose();
                    break;
                case Constants.EVENT_STOP_QUERY_SERVICE:
                    Timber.i("停止UpTimesService的定时器");
                    mDelay = UPDATE_DELAY;
                    dispose();
                    break;
            }
        }
    }
}